package com.slcf.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.slcf.common.ErrorEnum;
import com.slcf.controller.vo.ResultVO;
import com.slcf.service.CrQuestionnaireService;
import com.slcf.service.GuessService;

/**
 * 活动入口
 * @author Administrator
 *
 */
@Controller
public class ActivityController {

	@Autowired
	private GuessService guessService;
	
	@Autowired
	private CrQuestionnaireService crQuestionnaireService;
	
	/**
	 * 超级英雄竞猜
	 * @author zhangze
	 * @date 2017-06-01
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/guessHero.do", method = RequestMethod.GET)
	public void guessHero(HttpServletRequest request, HttpServletResponse response) {
		ResultVO result = null;
		try {
			String nickName = new String((request.getParameter("nickName")).getBytes("iso-8859-1"),"utf-8");
			String guessName = new String((request.getParameter("guessName")).getBytes("iso-8859-1"),"utf-8");
			String heroImgId = new String((request.getParameter("heroImgId")).getBytes("iso-8859-1"),"utf-8");
			if (StringUtils.isEmpty(nickName)) {
				result = ErrorEnum.NICKNAME_NULL.getResult();
			}
			if (nickName.length() >= 30) {
				result = ErrorEnum.NICKNAME_LENGTH_ERROR.getResult();
			}
			if (StringUtils.isEmpty(guessName)) {
				result = ErrorEnum.GUESSNAME_NULL.getResult();
			}
			if (guessName.length() >= 30) {
				result = ErrorEnum.GUESSNAME_LENGTH_ERROR.getResult();
			}
			if (result == null) {
				guessService.guessHero(nickName, guessName, heroImgId);
				result = ErrorEnum.SUCCESS.getResult();
			}
			System.out.println("超级英雄竞猜信息:nickName=" + nickName + " | guessName=" + guessName + " | heroImgId=" + heroImgId);
			response.setContentType("application/json;charset=UTF-8");//防止数据传递乱码
			response.getWriter().write(JSON.toJSONString(result));
			response.getWriter().flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * C罗问卷调查
	 * @author zhangze
	 * @date 2017-06-01
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/questionnaire.do", method = RequestMethod.GET)
	public void questionnaire(HttpServletRequest request, HttpServletResponse response) {
		ResultVO result = null;
		try {
			String nickName = new String((request.getParameter("nickName")).getBytes("iso-8859-1"),"utf-8");
			String answer = new String((request.getParameter("answer")).getBytes("iso-8859-1"),"utf-8");
			if (StringUtils.isEmpty(nickName)) {
				result = ErrorEnum.IS_NOT_NULL.getResult();
			}
			if (nickName.length() >= 30) {
				result = ErrorEnum.NICKNAME_LENGTH_ERROR.getResult();
			}
			if (StringUtils.isEmpty(answer)) {
				result = ErrorEnum.IS_NOT_NULL.getResult();
			}
			if (result == null) {
				crQuestionnaireService.questionnaire(nickName, answer);
				result = ErrorEnum.SUCCESS.getResult();
			}
			System.out.println("C罗问卷调查信息:nickName=" + nickName + " | answer=" + answer);
			response.setContentType("application/json;charset=UTF-8");//防止数据传递乱码
			response.getWriter().write(JSON.toJSONString(result));
			response.getWriter().flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
